<template>
	<view class="content">
		<u-navbar title="钱包" :border-bottom="false" back-icon-color="#ffffff" title-color="#fff"
			:background="{backgroundColor:'#2A2931'}"></u-navbar>
		<view class="main">
			<view class="tip" v-html="tixianText">
			</view>
			<view class="show">
				<view class="yue">
					<view class="label">账户余额</view>
					<view class="value"><text>{{volley}}</text>币</view>
				</view>
				<view class="yue">
					<view class="label">可提现</view>
					<view class="value"><text>{{volley}}</text>币</view>
				</view>
			</view>
			<u-cell-item :border-bottom="false" hover-class="none" title="微信信息 :" @click="zhifubao"
				:value="userinfo.zhifucount | returnname"></u-cell-item>
			<u-cell-item :border-bottom="false" hover-class="none" title="充值记录" @click="nav('rechargelog')">
			</u-cell-item>
			<u-cell-item :border-bottom="false" hover-class="none" title="提现记录" @click="nav('withdrawallog')">
			</u-cell-item>
		</view>
		<view class="footer">
			<view @click="rechargeshow = true">充值</view>
			<view class="tixian" @click="tixian">提现</view>
		</view>
		<u-keyboard ref="uKeyboard" :dot-enabled="false" @cancel="salepwdcancel" :tooltip="false" @change="valChange"
			@backspace="backspace" mode="number" v-model="Keyboardshow">
			<view>
				<view class="u-text-center u-padding-20 money">{{salepwdsyz?'再次输入密码':'设置支付密码'}}
				</view>
				<view class="u-flex u-row-center">
					<u-message-input mode="box" :maxlength="6" :dot-fill="true" v-model="salepwds"
						:disabled-keyboard="true"></u-message-input>
				</view>
			</view>
		</u-keyboard>
		<u-popup class="rechargepopup" v-model="rechargeshow" mode="bottom">
			<view class="recharge">
				<view class="top flex-bt"><text class="title new-theme-color">支付</text>
					<view class="flex"><text>可用余额：<text class="new-theme-color">{{volley}}</text> | </text>
						<image @click="getvolley" src="../../static/m5.png" class="reflash" />
					</view>
				</view>
				<view class="ul">
					<view class="list" v-for="item in topupcate" :key="item.price">
						<view class="left flex-c">
							<image src="../../static/m6.png" class="money-icon"></image> <text
								class="name new-margin-l-10">{{item.money}}</text>
						</view>
						<view @click="addorder(item.id, item.price)" class="btn">￥{{item.price}}</view>
					</view>
				</view>
			</view>
		</u-popup>
		<u-modal v-model="zfbshow" :show-title="false" show-cancel-button @confirm="nav('bindzfb')" confirm-text="修改">
			<u-field label-width="0" :value="userinfo.zhifuname"></u-field>
			<u-field label-width="0" :value="userinfo.zhifucount"></u-field>
		</u-modal>
	</view>
</template>

<script>
	import JSEncrypt from '@/common/jsencrypt.js'
	export default {
		data() {
			return {
				zfbshow: false,
				rechargeshow: false,
				Keyboardshow: false,
				salepwds: '',
				salepwdsyz: '',
				volley: '0.00',
				userinfo: {},
				topupcate: [],
				tixianText: ''
			}
		},
		onLoad() {},
		onShow() {
			this.getuserinfo()
			this.getvolley()
			this.gettopupcate()
			this.$getPageContent(4, (res) => {
				this.tixianText = res.body
			})
		},
		filters: {
			returnname(val) {
				if (val) {
					var s1 = val.slice(0, 3);
					var s2 = val.slice(-3);
					var s = s1 + '**' + s2;
					return s;
				}
			}
		},
		methods: {
			setRSA(msg) {
				let cryptFirst = new JSEncrypt();
				let key =
					'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCpvdExvx5zybwFboU1NSjuIghiuXNQSNiJ27G8iETbJdhtibKvTlbyzBVhq0brhnG16MG29pf8055zwODYKpWC+hPVJTflzhcYEVEqRpstq9q2BHe75ErRgjYIQiNGZJ11czQUj82TgDP3xMKntpchel5J3XbYfGjMzG3nARoOpwIDAQAB'
				cryptFirst.setPublicKey(key, '10001');
				console.log(cryptFirst)
				return cryptFirst.encrypt(msg);
			},
			async addorder(id, money) {
				var userid = this.$store.state.userid;
				var param = this.setRSA('[userid={{' + userid + '}}][id={{' + id + '}}][paymethod={{' + 0 + '}}]');
				uni.showLoading({mask:true})
				const {
					data: res
				} = await this.$Request({
					url: 'apis/home/Pay/recharge',
					method: 'POST',
					data: {
						z: param
					}
				})
				if (res.code == 200) {
					this.$WxMiniprogramPay({
						money: money,
						number: res['dingdan'],
					})
				} else {
					this.$u.toast(res.msg);
				}
			},
			async getuserinfo() {
				const {
					data: res
				} = await this.$Request({
					url: 'api/user/info',
					data: {
						user_id: this.$store.state.userid,
						longitude: this.$store.state.longitude,
						latitude: this.$store.state.latitude
					}
				})
				if (res.code === 200) {
					this.userinfo = res.result
				}
			},
			async getvolley() {
				const {
					data: res
				} = await this.$Request({
					url: 'api/user/userVolley',
					data: {
						user_id: this.$store.state.userid
					}
				})
				this.volley = res.volley
			},
			async gettopupcate() {
				const {
					data: res
				} = await this.$Request({
					url: 'apis/index.php/home/pay/topupcate'
				})
				this.topupcate = res.data
			},
			zhifubao() {
				if (this.userinfo.zhifuname) {
					return this.zfbshow = true
				}
				if (!this.userinfo.phone) {
					this.nav('phone')
				} else {
					this.nav('bindzfb')
				}
			},
			// 按键被点击(点击退格键不会触发此事件)
			valChange(val) {
				if (!this.salepwdsyz) {
					if (this.salepwds.length > 5) return false
					this.salepwds += val;
					if (this.salepwds.length == 6) {
						this.salepwdsyz = this.salepwds
						this.salepwds = ''
					}
				} else {
					if (this.salepwds.length > 5) return false
					this.salepwds += val;
					if (this.salepwds.length == 6) {
						if (this.salepwdsyz == this.salepwds) {
							this.setZFpwd()
						} else {
							this.salepwdcancel()
							this.$u.toast('密码不一致，请重新输入')
						}
					}
				}
			},
			async setZFpwd() {
				const {
					data: res
				} = await this.$Request({
					url: 'apis/home/Withdraw/setPwd?userid=' + this.$store.state.userid + '&password=' + this
						.salepwds
				})
				if (res && res.code == 200) {
					this.$u.toast('设置成功');
					this.getuserinfo()
					this.salepwdconfirm()
				} else {
					this.$u.toast('设置失败');
				}
			},
			// 退格键被点击
			backspace() {
				// 删除salepwds的最后一个字符
				if (this.salepwds.length) this.salepwds = this.salepwds.substr(0, this.salepwds.length - 1);
				console.log(this.salepwds);
			},
			salepwdcancel() {
				this.salepwds = ''
				this.salepwdsyz = ''
			},
			salepwdconfirm() {
				this.Keyboardshow = false
				this.salepwdcancel()
			},
			nav(text) {
				uni.navigateTo({
					url: `/pages/${text}/index`
				})
			},
			tixian() {
				if (!parseInt(this.userinfo.paypwd)) {
					return this.Keyboardshow = true
				} else if (!this.userinfo.zhifucount) {
					if (!this.userinfo.phone) {
						this.nav('phone')
					} else {
						this.nav('bindzfb')
					}
					return;
				} else {
					uni.navigateTo({
						url: '/pages/withdrawal/index'
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	/deep/.u-cell_title {
		color: rgb(213, 157, 81);
	}

	/deep/.u-cell__value {
		color: rgb(213, 157, 81);
	}

	/deep/.u-icon__icon {
		color: rgb(213, 157, 81);
	}

	.content {
		background-color: #2A2931;
		min-height: 100vh;

		.main {
			padding: 30rpx;

			.tip {
				color: rgb(213, 157, 81);
			}

			.show {
				display: flex;
				padding: 30rpx;
				margin: 30rpx 0;

				.yue {
					width: 50%;
					text-align: center;

					.label {
						color: #fff;
						font-size: 24rpx;
					}

					.value {
						color: rgb(213, 157, 81);

						text {
							font-weight: bold;
							font-size: 48rpx;
							color: #fff;
						}
					}
				}
			}
		}

		.footer {
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100%;
			display: flex;
			background-color: #eee;
			color: rgb(168, 123, 56);
			height: 100rpx;

			view {
				width: 50%;
				text-align: center;
				line-height: 100rpx;

				&.tixian {
					background-color: rgb(168, 123, 56);
					color: #fff;
				}
			}
		}
	}

	.warp {
		display: flex;
		align-items: center;
		justify-content: center;
		height: 100%;

		.rect {
			background-color: #fff;

			.tips {
				font-size: 32rpx;
				text-align: center;
			}

			.notice {
				color: #f40;
				padding: 30rpx 0;
				text-align: center;
			}
		}
	}

	.recharge {
		width: 100%;
		background: #666;

		.top {
			position: sticky;
			top: 0;
			display: flex;
			justify-content: space-between;
			padding: 20rpx;
			z-index: 111111;
			color: #666666;
			font-size: 28rpx;

			.title {
				font-size: 32rpx;
				font-weight: bold;
				color: #fff;
			}

			.flex {
				display: flex;
				align-items: center;
				color: #fff;

				.reflash {
					width: 30rpx;
					height: 30rpx;
					margin-left: 10rpx;
				}
			}
		}

		.ul {
			max-height: 50vh;

			.list {
				display: flex;
				justify-content: space-between;
				align-items: center;
				color: #666666;
				font-size: 28rpx;
				padding: 20rpx;

				.flex-c {
					display: flex;
					align-items: center;
					justify-content: center;

					.money-icon {
						width: 50rpx;
						height: 50rpx;
					}

					.new-margin-l-10 {
						margin-left: 10px;
						color: #fff;
					}
				}

				.btn {
					background: #fff;
					color: rgb(168, 123, 56);
					font-size: 28rpx;
					border-radius: 6rpx;
					padding: 10rpx 0;
					text-align: center;
					width: 130rpx;
				}
			}
		}
	}
</style>
